package com.cos.shop.services.impl;

import com.cos.shop.common.Const;
import com.cos.shop.dao.CartDao;
import com.cos.shop.dao.ItemDao;
import com.cos.shop.dao.OrderDao;
import com.cos.shop.dao.impl.CartDaoImpl;
import com.cos.shop.dao.impl.ItemDaoImpl;
import com.cos.shop.dao.impl.OrderDaoImpl;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.Item;
import com.cos.shop.entity.Orders;
import com.cos.shop.services.OrderServices;
import com.cos.shop.util.RandomUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/2/25 10:25
 * @description:
 */
public class OrderServicesImpl implements OrderServices {
    private OrderDao od = new OrderDaoImpl();
    private CartDao cd = new CartDaoImpl();
    private ItemDao id = new ItemDaoImpl();

    @Override
    public void chageStatus(String oid) {
        try {
            int i = od.chageOrderStateByOid(oid,Const.Order.NO_COMMENT);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean orderPay(String oid) {
        try {
            int i = od.chageOrderStateByOid(oid,Const.Order.IS_PAY);
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Orders getOrderDeatil(String oid) {
        try {
            //1 链表查询单个的 order和地址
            Orders o =od.getOrderWithAdressByOid(oid);
            //2 访问item 使用oid获取订单详数据
            List<Item> itemList = id.getItemsByOid(oid);
            //3 把订单详数据赋值给 orders
            o.setItemList(itemList);
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Orders> getOrderListByUid(int uid) {
        try {
            return od.getOrderListByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Orders addOrder(int aid, int uid) {
        try {
            //1 创建order 对象
            Orders o = new Orders();
            //2 设置订单参数
            //生成按时间随机的订单编号
            o.setOid(RandomUtils.createOrderId());
            //设置地址
            o.setAid(aid);
            //设置uid
            o.setUid(uid);
            //设置时间
            o.setOtime(new Date());
            //设置订单状态
            o.setOstate(Const.Order.NOT_PAY);
            //设置总价变量 累计购物车的价格
            double sum = 0;
            //3.获取购物车数据
            List<Cart> cartList = cd.getCartByUid(uid);
            //创建itemList 用于准备转存数据到item
            List<Item> itemList=new ArrayList<>();
            //循环遍历
            for (Cart cart : cartList) {
                //4累加订单总金额
                sum+=cart.getCcount();
                //创建item
                Item i = new Item();
                //设置订单编号
                i.setOid(o.getOid());
                //数据转存
                i.setPid(cart.getPid());
                i.setInum(cart.getCnum());
                i.setIcount(cart.getCcount());
                //把数据放入list
                itemList.add(i);
            }
            //5 设置订单的总金额
            o.setOcount(sum);
            //6 新增orders数据
            od.addOrder(o);
            //7 转存到item 注意item里面有orders的主键作为外键使用 ,所以需要在order创建完后再操作
            id.addItems(itemList);
            //8 删除用户所有购物车数据
            cd.getCartByUid(uid);
            //返回orders对象
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
